const app = getApp();
var url = app.data.url;
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    errorLog: '',
    phone: '',
    sms: '',
    code: '',
    uuid: '',
    btnType:0,
    time:60000,
    timeData: {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _that = this;
    wx.setNavigationBarTitle({
      title: "绑定手机"
    })
  },
  timeOnChange: function(opsions){
    let _that = this;
    _that.setData({
      timeData: opsions.detail
    })
  },
  finished: function(opsions){
    let _that = this;
    _that.setData({
      btnType: 0
    })
  },
  changePhone: function(options) {
    let _that = this;
    _that.setData({
      phone: options.detail
    })
  },
  changeMms: function(options) {
    let _that = this;
    _that.setData({
      sms: options.detail
    })
  },
  /**
   * 发送验证码
   */
  setMms: function(options) {
    let _that = this;
    let phone = _that.data.phone;
    if (!util.checkPhone(phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号!'
      })
      return false;
    }
    let uuid = app.globalData.openId;
    if (uuid == '') {
      wx.showToast({
        icon: 'none',
        title: '请先登录!'
      })
      return false;
    }
    //发起网络请求
    wx.request({
      url: url + '/WeChatLoginApi/sendSms',
      data: {
        phone: phone,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "发送成功!",
            icon: "success",
            duration: 2000
          })
          let code = res.data.data;
          _that.setData({
            code: code, 
            btnType: 1
          })
        } else {
          wx.showToast({
            title: '发送失败!',
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function(res) {
        console.info('submit fail');
      },
      complete: function(res) {
        console.info('submit complete');
      }
    })

  },
  /**
   * 绑定手机
   */
  bindPhone: function(options) {
    let _that = this;
    let phone = _that.data.phone;
    let sms = _that.data.sms;
    let uuid = app.globalData.openId;
    if(uuid == ''){
      wx.showToast({
        icon: 'none',
        title: '请先登录!'
      })
      return false;
    }
    console.log(uuid);
    if (!util.checkPhone(phone)) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的手机号!'
      })
      return false;
    }
    if (sms == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入验证码!'
      })
      return false;
    }


    wx.request({
      url: url + '/WeChatLoginApi/checkSms',
      data: {
        phone: phone,
        code: sms,
        uuid: uuid
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: "绑定成功!",
            icon: "success",
            duration: 2000
          })
          let code = res.data.data;
          _that.setData({
            code: code
          })
        } else {
          wx.showToast({
            title: '绑定失败!',
            icon: "none",
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.info('submit fail');
      },
      complete: function (res) {
        console.info('submit complete');
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let pageType = app.globalData.pageType;
    console.log("pageType:" + pageType);
    let _that = this;
    _that.setData({
      pageType: pageType
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})